//
//  INtroduceCourseTableViewController.m
//  Arts1
//
//  Created by 孤星晨宇 on 15/9/16.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "INtroduceCourseTableViewController.h"
#import "CourseDetailViewController.h"
#import "VideoRelatedTableViewCell.h"
#import "CourseImageTableViewCell.h"
#import "CourseTextTableViewCell.h"
#import "RelatedTableViewCell.h"
#import "TextTableViewCell.h"
#import "TeacherModel.h"
#import "CourseModel.h"
#import "AllHeader.h"

@interface INtroduceCourseTableViewController ()

@property (nonatomic, strong) NSArray *titleArr; // 标题

@property (nonatomic, assign) NSInteger picNum;

@property (nonatomic, strong) CourseDetailViewController *courseDVC;

@end

@implementation INtroduceCourseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = kBackgroundColor;
    // Do any additional setup after loading the view.
    _titleArr = @[@"讲师",@"课程介绍",@"视频截图",@"相关课程"];
//    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self createMainTableview];
}

- (void)setDetail:(CourseDetailModel *)detail{
    _detail = detail;
    if (_detail.photoArr) {
        _picNum = _detail.photoArr.count;
    } else {
        _picNum = 0;
    }
    [self.tableView reloadData];
    
}


- (void)createMainTableview{
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[CourseImageTableViewCell class] forCellReuseIdentifier:@"imagecell"];
    
    [self.tableView registerClass:[CourseTextTableViewCell class] forCellReuseIdentifier:@"textcell"];
    
    [self.tableView registerClass:[RelatedTableViewCell class] forCellReuseIdentifier:@"refrencecell"];
    
    [self.tableView registerClass:[TextTableViewCell class] forCellReuseIdentifier:@"titlecell"];
    
    [self.tableView registerClass:[VideoRelatedTableViewCell class] forCellReuseIdentifier:@"videoTableCell"];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6 + _picNum + _detail.courseArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 45;
    } else if (indexPath.row == 1) {
        return 56;
    } else if (indexPath.row == 3) {
        return [CourseTextTableViewCell heightForRow:_detail.intro];
    } else  if (indexPath.row == 2 || indexPath.row == 4 || indexPath.row == 5 + _detail.photoArr.count){
        return 40;
    } else if (indexPath.row > 4 && indexPath.row < 5 + _detail.photoArr.count){
        // 视频截图相应的cell高度
        return 9 * (SCREEN_WIDTH - 20) / 16.0 + 0.5;
    } else{
        // 相关课程相应的cell高度
       return  9 * (SCREEN_WIDTH - 20) / 32.0;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row== 1) {
        CourseImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imagecell" forIndexPath:indexPath];
        if (_detail.teacher) {
            cell.teacher = _detail.teacher;
        }
        return cell;
    } else if (indexPath.row == 3){
        CourseTextTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"textcell" forIndexPath:indexPath];
        cell.str = _detail.intro;
        CGFloat cellHeight = [CourseTextTableViewCell heightForRow:_detail.intro];
        cell.lineView.frame = CGRectMake(0, cellHeight - 0.5, cell.whiteView.size.width, 0.5);
        
        return cell;
        
    } else if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 4 || indexPath.row == 5 + _detail.photoArr.count){
        TextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"titlecell" forIndexPath:indexPath];
        cell.index = indexPath.row;
        if (indexPath.row == 5 + _detail.photoArr.count) {
            cell.text = _titleArr[3];
        } else {
        cell.text = _titleArr[indexPath.row/2];
        }
        return cell;
     
    } else if (indexPath.row > 4 && indexPath.row < 5 + _detail.photoArr.count){
        // 视频截图相应的cell
        VideoRelatedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"videoTableCell" forIndexPath:indexPath];
        if (_detail.photoArr.count) {
            cell.photourl = _detail.photourl;
            NSArray *arr = _detail.photoArr;
            cell.imageURL = arr[indexPath.row - 5];
        }
        cell.layer.masksToBounds = YES;
        
        return cell;
    } else {
        // 相关课程相应的cell
        RelatedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"refrencecell" forIndexPath:indexPath];
        cell.backgroundColor = kBackgroundColor;
        if (_detail.courseArr.count) {
            cell.photourl = _detail.photourl;
            NSArray *array = _detail.courseArr;
            cell.course = (CourseModel *)array[indexPath.row - 6 - _detail.photoArr.count];
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row >= 5 + _detail.photoArr.count) {
        NSArray *array = _detail.courseArr;
        CourseModel *course = (CourseModel *)array[indexPath.row - 6 - _detail.photoArr.count];
        _courseDVC = [[CourseDetailViewController alloc] init];
        _courseDVC.model = course;
        
        [_courseDVC.navigationview.backButton addTarget:self action:@selector(backCourse) forControlEvents:(UIControlEventTouchUpInside)];
        _courseDVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:_courseDVC animated:YES];
        _courseDVC.hidesBottomBarWhenPushed = YES;
        NSLog(@" 课程 和 视频详情  中的 相关课程跳转  测试   ");
    }
}

// 后退
- (void)backCourse
{
    //    self.tabBarController.tabBar.hidden = NO;
//    [_playController stop];
    //    [self.navigationController popViewControllerAnimated:YES];
//    [self dismissViewControllerAnimated:YES completion:nil];
    [_courseDVC.view removeFromSuperview];
}





/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
